var rowstate = new Array();
var callbacks = new Array();
            
function hideRow(rowId) {
	document.getElementById(rowId).style.display = "none";
}

function showRow(rowId) {
	document.getElementById(rowId).style.display = "";
}

function hasChildren(rowId) {
	return (document.getElementById(rowId + '.0') != null);
}
            
function getRowChildren(rowId) {
        var arr = new Array();
        i = 0;
        while (true) {
        	childRowId = rowId + '.' + i;
        	childEl = document.getElementById(childRowId);
        	if (childEl) {
        	        arr[i] = childRowId;
        	} else {
        	        break;
        	}
        	i++;
        }
        return (arr);
}
            
function toggle_row(rowId, state, force) {
	var rowChildren;
        var i;
        // open or close all children rows depend on current state
        force = (force == null) ? 1 : force; 
        if (state == null) {
        	row_state = ((rowstate[rowId]) ? (rowstate[rowId]) : 1) * -1;
        } else {
	        row_state = state;
        }
        rowChildren = getRowChildren(rowId);
        if (rowChildren.length == 0) return (false);
        for (i = 0; i < rowChildren.length; i++) {
        	if (row_state == -1) {
        	        hideRow(rowChildren[i]);
        	        toggle_row(rowChildren[i], row_state, -1);
        	} else {
        	        if (force == 1 || rowstate[rowId] != -1) {
        		        showRow(rowChildren[i]);
        	        toggle_row(rowChildren[i], row_state, -1);
        	        }
        	}
        }
        if (force == 1) {
        	rowstate[rowId] = row_state;
	        fireEventRowStateChanged(rowId, row_state);
        }
        return (true);
}
            
function fireEventRowStateChanged(rowId, state) {
        if (callbacks['eventRowStateChanged']) {
	        callback = callbacks['eventRowStateChanged'] + "('" + rowId + "', " + state + ");";
	        eval(callback);
        }
}

function collapseAll(tableId) {
        table = document.getElementById(tableId);
        rowChildren = table.getElementsByTagName('tr');
        for (i = 0; i < rowChildren.length; i++) {
        	if (index = rowChildren[i].id.indexOf('.')) {
                	// do not hide root elements
                	if(index != rowChildren[i].id.lastIndexOf('.')) {
                		rowChildren[i].style.display = 'none';
                	}
        		if (hasChildren(rowChildren[i].id)) {
                		rowstate[rowChildren[i].id] = -1;
                		fireEventRowStateChanged(rowChildren[i].id, -1);
                	}
        	}
        }
        return (true);
}    

function expandAll(tableId) {
	table = document.getElementById(tableId);
	rowChildren = table.getElementsByTagName('tr');
	for (i = 0; i < rowChildren.length; i++) {
		if (index = rowChildren[i].id.indexOf('.')) {
		        rowChildren[i].style.display = '';
		        if (hasChildren(rowChildren[i].id)) {
			        rowstate[rowChildren[i].id] = 1;
			        fireEventRowStateChanged(rowChildren[i].id, 1);
		        }
		}
	}
	return (true);
}

callbacks['eventRowStateChanged'] = 'treetable_eventRowChanged';

function treetable_eventRowChanged(rowId, state) {
        img = document.getElementById(rowId).getElementsByTagName('td')[0].getElementsByTagName('img')[0];
        if (state == 1) {
	        img.src = 'folder_green_open.png';
        } else {
	        img.src = 'folder_green.png';
        }
        return (true);
}  
